home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac Pascal Primer, 4.0 / Chap 7, WindowMaker ƒ / WindowMaker.p next >
Text File  |  1990-07-27  |  8KB  |  365 lines

  1. program WindowMaker;
  2.     const
  3.         BASE_RES_ID = 400;
  4.  
  5.         APPLE_MENU_ID = 400;
  6.         FILE_MENU_ID = 401;
  7.         EDIT_MENU_ID = 402;
  8.  
  9.         ABOUT_ITEM = 1;
  10.         ABOUT_ALERT = 400;
  11.         ERROR_ALERT_ID = 401;
  12.  
  13.         NO_MBAR = BASE_RES_ID;
  14.         NO_MENU = BASE_RES_ID + 1;
  15.         NO_PICTURE = BASE_RES_ID + 2;
  16.         NO_WIND = BASE_RES_ID + 3;
  17.  
  18.         NEW_ITEM = 1;
  19.         CLOSE_ITEM = 2;
  20.         QUIT_ITEM = 3;
  21.  
  22.         UNDO_ITEM = 1;
  23.         CUT_ITEM = 3;
  24.         COPY_ITEM = 4;
  25.         PASTE_ITEM = 5;
  26.         CLEAR_ITEM = 6;
  27.  
  28.         EDGE_THRESHOLD = 30;
  29.  
  30.         WINDOW_HOME_LEFT = 5;
  31.         WINDOW_HOME_TOP = 45;
  32.         NEW_WINDOW_OFFSET = 20;
  33.  
  34.         MIN_SLEEP = 60;
  35.  
  36.         LEAVE_WHERE_IT_IS = FALSE;
  37.  
  38.         WNE_TRAP_NUM = $60;
  39.         UNIMPL_TRAP_NUM = $9F;
  40.  
  41.         NIL_STRING = '';
  42.         HOPELESSLY_FATAL_ERROR = 'Game over, man!';
  43.  
  44.     var
  45.         gDone, gWNEImplemented: Boolean;
  46.         gTheEvent: EventRecord;
  47.         gNewWindowLeft, gNewWindowTop: INTEGER;
  48.  
  49.  
  50. {-------------------------------->    ErrorHandler    <---}
  51.  
  52.     procedure ErrorHandler (stringNum: INTEGER);
  53.         var
  54.             errorStringH: StringHandle;
  55.             dummy: INTEGER;
  56.     begin
  57.         errorStringH := GetString(stringNum);
  58.         if errorStringH = nil then
  59.             ParamText(HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING)
  60.         else
  61.             ParamText(errorStringH^^, NIL_STRING, NIL_STRING, NIL_STRING);
  62.  
  63.         dummy := StopAlert(ERROR_ALERT_ID, nil);
  64.         ExitToShell;
  65.     end;
  66.  
  67.  
  68. {-------------------------------->    CenterPict    <---}
  69.  
  70.     procedure CenterPict (thePicture: PicHandle; var myRect: Rect);
  71.         var
  72.             windRect, pictureRect: Rect;
  73.     begin
  74.         windRect := myRect;
  75.         pictureRect := thePicture^^.picFrame;
  76.         myRect.top := (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top)) div 2 + windRect.top;
  77.         myRect.bottom := myRect.top + (pictureRect.bottom - pictureRect.top);
  78.         myRect.left := (windRect.right - windRect.left - (pictureRect.right - pictureRect.left)) div 2 + windRect.left;
  79.         myRect.right := myRect.left + (pictureRect.right - pictureRect.left);
  80.     end;
  81.  
  82.  
  83. {-------------------------------->    DrawMyPicture    <---}
  84.  
  85.     procedure DrawMyPicture (pictureWindow: WindowPtr);
  86.         var
  87.             myRect: Rect;
  88.             thePicture: PicHandle;
  89.     begin
  90.         myRect := pictureWindow^.portRect;
  91.  
  92.         thePicture := GetPicture(BASE_RES_ID);
  93.         if thePicture = nil then
  94.             ErrorHandler(NO_PICTURE);
  95.  
  96.         CenterPict(thePicture, myRect);
  97.         SetPort(pictureWindow);
  98.         DrawPicture(thePicture, myRect);
  99.     end;
  100.  
  101.  
  102. {-------------------------------->    CreateWindow    <---}
  103.  
  104.     procedure CreateWindow;
  105.         var
  106.             theNewestWindow: WindowPtr;
  107.     begin
  108.         theNewestWindow := GetNewWindow(BASE_RES_ID, nil, WindowPtr(-1));
  109.         if theNewestWindow = nil then
  110.             ErrorHandler(NO_WIND);
  111.  
  112.         if ((screenBits.bounds.right - gNewWindowLeft) < EDGE_THRESHOLD) or ((screenBits.bounds.bottom - gNewWindowTop) < EDGE_THRESHOLD) then
  113.             begin
  114.                 gNewWindowLeft := WINDOW_HOME_LEFT;
  115.                 gNewWindowTop := WINDOW_HOME_TOP;
  116.             end;
  117.  
  118.         MoveWindow(theNewestWindow, gNewWindowLeft, gNewWindowTop, LEAVE_WHERE_IT_IS);
  119.         gNewWindowLeft := gNewWindowLeft + NEW_WINDOW_OFFSET;
  120.         gNewWindowTop := gNewWindowTop + NEW_WINDOW_OFFSET;
  121.         ShowWindow(theNewestWindow);
  122.     end;
  123.  
  124.  
  125. {-------------------------------->    HandleEditChoice    <---}
  126.  
  127.     procedure HandleEditChoice (theItem: INTEGER);
  128.         var
  129.             dummy: Boolean;
  130.     begin
  131.         dummy := SystemEdit(theItem - 1);
  132.     end;
  133.  
  134.  
  135. {-------------------------------->    HandleFileChoice    <---}
  136.  
  137.     procedure HandleFileChoice (theItem: INTEGER);
  138.         var
  139.             whichWindow: WindowPtr;
  140.     begin
  141.         case theItem of
  142.             NEW_ITEM: 
  143.                 CreateWindow;
  144.             CLOSE_ITEM: 
  145.                 begin
  146.                     whichWindow := FrontWindow;
  147.                     if whichWindow <> nil then
  148.                         DisposeWindow(whichWindow);
  149.                 end;
  150.             QUIT_ITEM: 
  151.                 gDone := TRUE;
  152.         end;
  153.     end;
  154.  
  155.  
  156. {-------------------------------->    HandleAppleChoice    <---}
  157.  
  158.     procedure HandleAppleChoice (theItem: INTEGER);
  159.         var
  160.             accName: Str255;
  161.             accNumber, itemNumber, dummy: INTEGER;
  162.             aMenu: MenuHandle;
  163.     begin
  164.         case theItem of
  165.             ABOUT_ITEM: 
  166.                 dummy := NoteAlert(ABOUT_ALERT, nil);
  167.             otherwise
  168.                 begin
  169.                     aMenu := GetMHandle(APPLE_MENU_ID);
  170.                     GetItem(aMenu, theItem, accName);
  171.                     accNumber := OpenDeskAcc(accName);
  172.                 end;
  173.         end;
  174.     end;
  175.  
  176.  
  177. {-------------------------------->    HandleMenuChoice    <---}
  178.  
  179.     procedure HandleMenuChoice (menuChoice: LONGINT);
  180.         var
  181.             theMenu, theItem: INTEGER;
  182.     begin
  183.         if menuChoice <> 0 then
  184.             begin
  185.                 theMenu := HiWord(menuChoice);
  186.                 theItem := LoWord(menuChoice);
  187.  
  188.                 case theMenu of
  189.                     APPLE_MENU_ID: 
  190.                         HandleAppleChoice(theItem);
  191.                     FILE_MENU_ID: 
  192.                         HandleFileChoice(theItem);
  193.                     EDIT_MENU_ID: 
  194.                         HandleEditChoice(theItem);
  195.                 end;
  196.  
  197.                 HiliteMenu(0);
  198.             end;
  199.     end;
  200.  
  201.  
  202. {-------------------------------->    IsDAWindow    <---}
  203.  
  204.     function IsDAWindow (whichWindow: WindowPtr): BOOLEAN;
  205.     begin
  206.         if whichWindow = nil then
  207.             IsDAWindow := FALSE
  208.         else
  209.             IsDAWindow := (WindowPeek(whichWindow)^.windowKind < 0);
  210.     end;
  211.  
  212.  
  213. {-------------------------------->    AdjustMenus    <---}
  214.  
  215.     procedure AdjustMenus;
  216.         var
  217.             aMenu: MenuHandle;
  218.     begin
  219.         aMenu := GetMHandle(FILE_MENU_ID);
  220.         if FrontWindow = nil then
  221.             DisableItem(aMenu, CLOSE_ITEM)
  222.         else
  223.             EnableItem(aMenu, CLOSE_ITEM);
  224.  
  225.         aMenu := GetMHandle(EDIT_MENU_ID);
  226.         if IsDAWindow(FrontWindow) then
  227.             begin
  228.                 EnableItem(aMenu, UNDO_ITEM);
  229.                 EnableItem(aMenu, CUT_ITEM);
  230.                 EnableItem(aMenu, COPY_ITEM);
  231.                 EnableItem(aMenu, PASTE_ITEM);
  232.                 EnableItem(aMenu, CLEAR_ITEM);
  233.             end
  234.         else
  235.             begin
  236.                 DisableItem(aMenu, UNDO_ITEM);
  237.                 DisableItem(aMenu, CUT_ITEM);
  238.                 DisableItem(aMenu, COPY_ITEM);
  239.                 DisableItem(aMenu, PASTE_ITEM);
  240.                 DisableItem(aMenu, CLEAR_ITEM);
  241.             end;
  242.     end;
  243.  
  244.  
  245. {-------------------------------->    HandleMouseDown    <---}
  246.  
  247.     procedure HandleMouseDown;
  248.         var
  249.             whichWindow: WindowPtr;
  250.             thePart: INTEGER;
  251.             menuChoice, windSize: LONGINT;
  252.     begin
  253.         thePart := FindWindow(gTheEvent.where, whichWindow);
  254.         case thePart of
  255.             inMenuBar: 
  256.                 begin
  257.                     AdjustMenus;
  258.                     menuChoice := MenuSelect(gTheEvent.where);
  259.                     HandleMenuChoice(menuChoice);
  260.                 end;
  261.             inSysWindow: 
  262.                 SystemClick(gTheEvent, whichWindow);
  263.             inDrag: 
  264.                 DragWindow(whichWindow, gTheEvent.where, screenBits.bounds);
  265.             inGoAway: 
  266.                 DisposeWindow(whichWindow);
  267.             inContent: 
  268.                 SelectWindow(whichWindow);
  269.         end;
  270.     end;
  271.  
  272.  
  273. {-------------------------------->    HandleEvent    <---}
  274.  
  275.     procedure HandleEvent;
  276.         var
  277.             theChar: CHAR;
  278.             dummy: BOOLEAN;
  279.             oldPort: GrafPtr;
  280.     begin
  281.         if gWNEImplemented then
  282.             dummy := WaitNextEvent(everyEvent, gTheEvent, MIN_SLEEP, nil)
  283.         else
  284.             begin
  285.                 SystemTask;
  286.                 dummy := GetNextEvent(everyEvent, gTheEvent);
  287.             end;
  288.  
  289.         case gTheEvent.what of
  290.             mouseDown: 
  291.                 HandleMouseDown;
  292.             keyDown, autoKey: 
  293.                 begin
  294.                     theChar := CHR(BitAnd(gTheEvent.message, charCodeMask));
  295.                     if (BitAnd(gTheEvent.modifiers, cmdKey) <> 0) then
  296.                         begin
  297.                             AdjustMenus;
  298.                             HandleMenuChoice(MenuKey(theChar));
  299.                         end;
  300.                 end;
  301.             updateEvt: 
  302.                 if not IsDAWindow(WindowPtr(gTheEvent.message)) then
  303.                     begin
  304.                         GetPort(oldPort);
  305.                         SetPort(WindowPtr(gTheEvent.message));
  306.                         BeginUpdate(WindowPtr(gTheEvent.message));
  307.                         DrawMyPicture(WindowPtr(gTheEvent.message));
  308.                         EndUpdate(WindowPtr(gTheEvent.message));
  309.                         SetPort(oldPort);
  310.                     end;
  311.         end;
  312.     end;
  313.  
  314.  
  315. {-------------------------------->    MainLoop    <---}
  316.  
  317.     procedure MainLoop;
  318.     begin
  319.         gDone := FALSE;
  320.         gNewWindowLeft := WINDOW_HOME_LEFT;
  321.         gNewWindowTop := WINDOW_HOME_TOP;
  322.  
  323.         gWNEImplemented := (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) <> NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
  324.         while (gDone = FALSE) do
  325.             HandleEvent;
  326.     end;
  327.  
  328.  
  329. {-------------------------------->    MenuBarInit    <---}
  330.  
  331.     procedure MenuBarInit;
  332.         var
  333.             myMenuBar: Handle;
  334.             aMenu: MenuHandle;
  335.     begin
  336.         myMenuBar := GetNewMBar(BASE_RES_ID);
  337.         if myMenuBar = nil then
  338.             ErrorHandler(NO_MBAR);
  339.         SetMenuBar(myMenuBar);
  340.  
  341.         aMenu := GetMHandle(APPLE_MENU_ID);
  342.         if aMenu = nil then
  343.             ErrorHandler(NO_MENU);
  344.  
  345.         AddResMenu(aMenu, 'DRVR');
  346.  
  347.         aMenu := GetMHandle(EDIT_MENU_ID);
  348.         if aMenu = nil then
  349.             ErrorHandler(NO_MENU);
  350.  
  351.         aMenu := GetMHandle(FILE_MENU_ID);
  352.         if aMenu = nil then
  353.             ErrorHandler(NO_MENU);
  354.  
  355.         DrawMenuBar;
  356.     end;
  357.  
  358.  
  359. {-------------------------------->    WindowMaker    <---}
  360.  
  361. begin
  362.     MenuBarInit;
  363.  
  364.     MainLoop;
  365. end.